package com.cntaiping.workutil.aimanager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;

import java.util.ArrayList;
import java.util.List;

public class RedisLua {

    private static final String SCRIPT = "local times = redis.call('incr',KEYS[1]);if times==1 then redis.call('expire',KEYS[1],tonumber(ARGV[1])) end; return times;";

    @Autowired
    private StringRedisTemplate redisTemplate;


    public Long concurrence(String keys,String expireTime){

        DefaultRedisScript<Long> script = new DefaultRedisScript<>();
        script.setScriptText(SCRIPT);
        script.setResultType(Long.class);

        List<String> key = new ArrayList<>();
        key.add(keys);

        return redisTemplate.execute(script,key,expireTime);
    }
}
